
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="Zy">
    <script src="jquery-3.1.1.js"></script>
    <title>jquery自动轮播图</title>
    <style>
        ul,li{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        #box{
            width: 520px;
            height: 280px;
            margin: 100px auto;
            position: relative;
        }
        #box .list li{
            position: absolute;
            top: 0;
            left: 0;
            display: none;
        }
        #box .list li.current{
            display: block;
        }
        #box .count{
            position: absolute;
            left: 10px;
            bottom: 10px;
        }
        #box .count li{
            float: left;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #fa0;
            text-align: center;
            line-height: 20px;
            margin-left: 10px;
            color: #fff;
            opacity: 0.8;
            cursor: pointer;
        }
        #box .count li.current{
            background-color: #f60;
            opacity: 1;
        }
        #box .jiantou{
            position: absolute;
            right: 10px;
            bottom: 10px;
        }
        #box .jiantou li{
            float: left;
            margin-right: 10px;
            width: 30px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            font-size: 26px;
            font-weight: bold;
            color: white;
            background-color: grey;
            opacity: 0.8;
            cursor: pointer;
        }
        /*#box .jiantou div{
            width: 30px;
            height: 30px;
            position: absolute;
            background-color: grey;
            opacity: 0.5;
            z-index: 10;
        }
        #box .jiantou div:nth-of-type(1){
            top: 0;
            left: 0;
        }
        #box .jiantou div:nth-of-type(2){
            top: 0;
            left: 40px;
        }*/
    </style>
</head>
<body>
    <div id="box">
        <ul class="list"> 
            <li class="current"><img src="img/1.jpg" alt=""></li>
            <li><img src="img/2.jpg" alt=""></li>
            <li><img src="img/3.jpg" alt=""></li>
            <li><img src="img/4.jpg" alt=""></li>
            <li><img src="img/5.jpg" alt=""></li>
        </ul>
        <ul class="count">
            <li class="current">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
        <ul class="jiantou">
            <!--<div></div>-->
            <li><</li>
            <!--<div></div>-->
            <li>></li>
        </ul>
    </div>
</body>
</html>
<script>
    var current= 0;
    var timer = null;
    
    timer = setInterval(autoPlay,1000);
    // 自动轮播
    function autoPlay(){
        current++;
        if(current >= $('.list li').length){
            current = 0;
        }
        show(current);
    }
    // 手动轮播
    $('.count li').mouseover(function(){
        show($('.count li').index($(this)));
    })

    // 点击左侧箭头
    $('.jiantou li:first').click(function(){
        current--;
        if(current<0){
            current = $('.list li').length-1;
        }
        show(current);
    })
    // 点击右侧箭头
    $('.jiantou li:last').click(function(){
        current++;
        if(current >= $('.list li').length){
            current = 0;
        }
        show(current);
    })


    function show(index){
        current = index;
        $('.list li').each(function(){
            $(this).removeClass('current');
        })
        $('.count li').each(function(){
            $(this).removeClass('current');
        })
        $('.list li').eq(current).addClass('current');
        $('.count li').eq(current).addClass('current');
    }
    // 当鼠标移进或移出时，定时器清除或开启
    $('#box').mouseover(function(){
        clearInterval(timer);
    })
    $('#box').mouseout(function(){
        timer = setInterval(autoPlay,1000);
    })
</script>